import { createStore, useStore as useVuexStore, Store } from 'vuex'
import { IRootState } from './type'
import login from './login/login'
import system from './main/system/system'
import dashboard from './main/analysis/dashboard'

import { IStoreType } from './type'

import { getDepartmentList, getRoleList } from '@/service/main/system/index'

const store = createStore<IRootState>({
  state() {
    return {
      name: '',
      password: '',
      departmentList: [],
      roleList: []
    }
  },
  getters: {
  },
  mutations: {
    changePageDepartmentList(state, departmentList: []) {
      state.departmentList = departmentList
    },
    changePageRoleList(state, roleList: []) {
      state.roleList = roleList
    }
  },
  actions: {
    async getPageDepartmentListAction({ commit }, payload: any) {
      const res = await getDepartmentList('/department/list', {
        offset: 0,
        size: 1000
      })

      const { list } = res.data

      commit('changePageDepartmentList', list)
    },
    async getPageRoleListAction({ commit }, payload: any) {
      const res = await getRoleList('/role/list', {
        offset: 0,
        size: 1000
      })

      const { list } = res.data

      commit('changePageRoleList', list)
    },
  },
  modules: {
    login,
    system,
    dashboard
  }
})

export function setupStore() {
  store.dispatch('login/setUpLoginAction')
  store.dispatch('getPageDepartmentListAction')
  store.dispatch('getPageRoleListAction')
}

export function useStore(): Store<IStoreType> {
  return useVuexStore()
}

export default store